Trò chơi Tic-Tac-Toe, game đánh caro full source code
- PhotonStreamQueue.cs
- PhotonNetwork /
- Plugins /
- Photon Unity Networking /
- Assets /
- project /
2 using System.Collections;
3 using System.Collections.Generic;
4
5 /// <summary>
6 /// The PhotonStreamQueue helps you poll object states at higher frequencies then what
7 /// PhotonNetwork.sendRate dictates and then sends all those states at once when
8 /// Serialize() is called.
9 /// On the receiving end you can call Deserialize() and then the stream will roll out
10 /// the received object states in the same order and timeStep they were recorded in.
11 /// </summary>
12 public class PhotonStreamQueue
13 {
14 #region Members
15 int m_SampleRate;
16 int m_SampleCount;
17 int m_ObjectsPerSample = -1;
18
19 float m_LastSampleTime = -Mathf.Infinity;
20 int m_LastFrameCount = -1;
21 int m_NextObjectIndex = -1;
22
23 List<object> m_Objects = new List<object>();
24
25 bool m_IsWriting;
26 #endregion
27
28 /// <summary>
29 /// Initializes a new instance of the <see cref="PhotonStreamQueue"/> class.
30 /// </summary>
31 /// <param name="sampleRate">How many times per second should the object states be sampled</param>
32 public PhotonStreamQueue( int sampleRate )
33 {
34 m_SampleRate = sampleRate;
35 }
36
37 void BeginWritePackage()
38 {
39 //If not enough time has passed since the last sample, we don't want to write anything
40 if( Time.realtimeSinceStartup < m_LastSampleTime + 1f / m_SampleRate )
41 {
42 m_IsWriting = false;
43 return;
44 }
45
46 if( m_SampleCount == 1 )
47 {
48 m_ObjectsPerSample = m_Objects.Count;
49 //Debug.Log( "Setting m_ObjectsPerSample to " + m_ObjectsPerSample );
50 }
51 else if( m_SampleCount > 1 )
52 {
53 if( m_Objects.Count / m_SampleCount != m_ObjectsPerSample )
54 {
55 Debug.LogWarning( "The number of objects sent via a PhotonStreamQueue has to be the same each frame" );
56 Debug.LogWarning( "Objects in List: " + m_Objects.Count + " / Sample Count: " + m_SampleCount + " = " + ( m_Objects.Count / m_SampleCount ) + " != " + m_ObjectsPerSample );
57 }
58 }
59
60 /*if( m_SampleCount > 1 )
61 {
62 Debug.Log( "Check: " + m_Objects.Count + " / " + m_SampleCount + " = " + ( m_Objects.Count / m_SampleCount ) + " = " + m_ObjectsPerSample );
63 }*/
64
65 m_IsWriting = true;
66 m_SampleCount++;
67 m_LastSampleTime = Time.realtimeSinceStartup;
68
69 }
70
71 /// <summary>
72 /// Resets the PhotonStreamQueue. You need to do this whenever the amount of objects you are observing changes
73 /// </summary>
74 public void Reset()
75 {
76 m_SampleCount = 0;
77 m_ObjectsPerSample = -1;
78
79 m_LastSampleTime = -Mathf.Infinity;
80 m_LastFrameCount = -1;
81
82 m_Objects.Clear();
83 }
84
85 /// <summary>
86 /// Adds the next object to the queue. This works just like PhotonStream.SendNext
87 /// </summary>
88 /// <param name="obj">The object you want to add to the queue</param>
89 public void SendNext( object obj )
90 {
91 if( Time.frameCount != m_LastFrameCount )
92 {
93 BeginWritePackage();
94 }
95
96 m_LastFrameCount = Time.frameCount;
97
98 if( m_IsWriting == false )
99 {
100 return;
101 }
102
103 m_Objects.Add( obj );
104 }
105
106 /// <summary>
107 /// Determines whether the queue has stored any objects
108 /// </summary>
109 public bool HasQueuedObjects()
110 {
111 return m_NextObjectIndex != -1;
112 }
113
114 /// <summary>
115 /// Receives the next object from the queue. This works just like PhotonStream.ReceiveNext
116 /// </summary>
117 /// <returns></returns>
118 public object ReceiveNext()
119 {
120 if( m_NextObjectIndex == -1 )
121 {
122 return null;
123 }
124
125 if( m_NextObjectIndex >= m_Objects.Count )
126 {
127 m_NextObjectIndex -= m_ObjectsPerSample;
128 }
129
130 return m_Objects[ m_NextObjectIndex++ ];
131 }
132
133 /// <summary>
134 /// Serializes the specified stream. Call this in your OnPhotonSerializeView method to send the whole recorded stream.
135 /// </summary>
136 /// <param name="stream">The PhotonStream you receive as a parameter in OnPhotonSerializeView</param>
137 public void Serialize( PhotonStream stream )
138 {
139 stream.SendNext( m_SampleCount );
140 stream.SendNext( m_ObjectsPerSample );
141
142 for( int i = 0; i < m_Objects.Count; ++i )
143 {
144 stream.SendNext( m_Objects[ i ] );
145 }
146
147 //Debug.Log( "Serialize " + m_SampleCount + " samples with " + m_ObjectsPerSample + " objects per sample. object count: " + m_Objects.Count + " / " + ( m_SampleCount * m_ObjectsPerSample ) );
148
149 m_Objects.Clear();
150 m_SampleCount = 0;
151 }
152
153 /// <summary>
154 /// Deserializes the specified stream. Call this in your OnPhotonSerializeView method to receive the whole recorded stream.
155 /// </summary>
156 /// <param name="stream">The PhotonStream you receive as a parameter in OnPhotonSerializeView</param>
157 public void Deserialize( PhotonStream stream )
158 {
159 m_Objects.Clear();
160
161 m_SampleCount = (int)stream.ReceiveNext();
162 m_ObjectsPerSample = (int)stream.ReceiveNext();
163
164 for( int i = 0; i < m_SampleCount * m_ObjectsPerSample; ++i )
165 {
166 m_Objects.Add( stream.ReceiveNext() );
167 }
168
169 if( m_Objects.Count > 0 )
170 {
171 m_NextObjectIndex = 0;
172 }
173 else
174 {
175 m_NextObjectIndex = -1;
176 }
177
178 //Debug.Log( "Deserialized " + m_SampleCount + " samples with " + m_ObjectsPerSample + " objects per sample. object count: " + m_Objects.Count + " / " + ( m_SampleCount * m_ObjectsPerSample ) );
179 }
180 }
The PhotonStreamQueue helps you poll object states at higher frequencies then what
PhotonNetwork.sendRate dictates and then sends all those states at once when
Serialize() is called.
On the receiving end you can call Deserialize() and then the stream will roll out
the received object states in the same order and timeStep they were recorded in.
Initializes a new instance of the
How many times per second should the object states be sampled
If not enough time has passed since the last sample, we don't want to write anything
Debug.Log( "Setting m_ObjectsPerSample to " + m_ObjectsPerSample );
Resets the PhotonStreamQueue. You need to do this whenever the amount of objects you are observing changes
Adds the next object to the queue. This works just like PhotonStream.SendNext
The object you want to add to the queue
Determines whether the queue has stored any objects
Receives the next object from the queue. This works just like PhotonStream.ReceiveNext
Serializes the specified stream. Call this in your OnPhotonSerializeView method to send the whole recorded stream.
The PhotonStream you receive as a parameter in OnPhotonSerializeView
Debug.Log( "Serialize " + m_SampleCount + " samples with " + m_ObjectsPerSample + " objects per sample. object count: " + m_Objects.Count + " " + ( m_SampleCount * m_ObjectsPerSample ) );
Deserializes the specified stream. Call this in your OnPhotonSerializeView method to receive the whole recorded stream.
The PhotonStream you receive as a parameter in OnPhotonSerializeView
Debug.Log( "Deserialized " + m_SampleCount + " samples with " + m_ObjectsPerSample + " objects per sample. object count: " + m_Objects.Count + " " + ( m_SampleCount * m_ObjectsPerSample ) );